/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.panel.User;

import java.sql.*;
import handler.database;

/**
 *
 * @author ifan
 */
public class UserPanelServlet extends HttpServlet {
    database db = new database();
    ArrayList<User> listUser;
    
    @Override
    public void init() throws ServletException {
        super.init();

    }

    private void getDatabaseModerator() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        Connection conn = null;
        String strQuery = "";
        Statement statement = null;
        ResultSet result = null;
        
        database db = new database();
        conn = db.getConnection();
        strQuery = "Select * from user where role='moderator' ORDER BY username ASC";
        System.out.println(strQuery);
        statement = conn.createStatement();
        result = statement.executeQuery(strQuery);
        while (result.next()) {
            int id = Integer.parseInt(result.getString(1));
            String userName = result.getString(2);
            String password = result.getString(3);
            String email = result.getString(4);
            String role = result.getString(5);
            int max_number_room = Integer.parseInt(result.getString(6));
            int ban = Integer.parseInt(result.getString(7));
            User user = new User(id, userName, password, email, role, max_number_room, ban);
            listUser.add(user);
        }
        conn.close();
    }

    private void getDatabaseUser() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        Connection conn = null;
        String strQuery = "";
        Statement statement = null;
        ResultSet result = null;

        database db = new database();
        conn = db.getConnection();
        strQuery = "Select * from user where role='user' ORDER BY username ASC";
        System.out.println(strQuery);
        statement = conn.createStatement();
        result = statement.executeQuery(strQuery);
        while (result.next()) {
            int id = Integer.parseInt(result.getString(1));
            String userName = result.getString(2);
            String password = result.getString(3);
            String email = result.getString(4);
            String role = result.getString(5);
            int max_number_room = Integer.parseInt(result.getString(6));
            int ban = Integer.parseInt(result.getString(7));
            User user = new User(id, userName, password, email, role, max_number_room, ban);
            listUser.add(user);
        }
        conn.close();
    }


   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        //ServletContext sc = getServletContext();

        listUser = new ArrayList<User>();
        try {
            getDatabaseModerator();
            getDatabaseUser();
        } catch (SQLException ex) {
            Logger.getLogger(UserPanelServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(UserPanelServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(UserPanelServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserPanelServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        //sc.setAttribute("UserPanel", userPanel);
        
        try {
            Gson gson = new Gson();
            String users = gson.toJson(listUser);
            System.out.println(users);
            out.println(users);
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
